﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OnTheWay.Functions;
using Models;
using System.ComponentModel;
using LocalService;
using OnTheWay.Enums;

namespace PcBackgroundMgr.Functions
{
    /// <summary>
    /// UserModelCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class UserModelCtrl : UserModelCtrlBase
    {
        public UserModelCtrl()
        {
            InitializeComponent();
        }

        private Array _Levels = Enum.GetValues(typeof(Models.User.UserLevel));

        public Array Levels
        {
            get { return _Levels; }
            set { _Levels = value; }
        }

        private Array _Statuses = Enum.GetValues(typeof(Models.User.UserStatus));

        public Array Statuses
        {
            get { return _Statuses; }
            set { _Statuses = value; }
        }

    }

    [DesignTimeVisible(false)]
    public partial class UserModelCtrlBase : FunctionCtrlModel<User>
    {
        private static readonly string DEFAULT = "******";

        public string Password1 { get; set; }
        public string Password2 { get; set; }

        public UserModelCtrlBase()
            :base(LocalFactory.UserLocal)
        {
        }

        public override User OnInitializeModel(User model)
        {
            if (model == null)
            {
                model = new User();
                this.OperateMode = OperateModeEnum.Add;
                this.Title = "添加会员";
            }
            else
            {
                this.OperateMode = OperateModeEnum.Modify;
                this.Password1 = Password2 = DEFAULT;
                this.Title = "修改会员";
            }
            return model;
        }


        protected override bool OnCanSave(User model)
        {
            if (string.IsNullOrEmpty(Password1))
            {
                this.MessageStatus = MessageStatusEnum.Warning;
                this.Message = "请输入密码";
                return false;
            }
            if (!string.Equals(Password1, Password2))
            {
                this.MessageStatus = MessageStatusEnum.Warning;
                this.Message = "两次密码输入不一致";
                return false;
            }
            if (Password1 != DEFAULT)
            {
                model.Password = BlackMofoApp.App.EncryptionPassword(Password1);
            }
            return base.OnCanSave(model);
        }

    }

}
